package com.tiocloud.account.feature.login;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.tiocloud.account.R;
import com.tiocloud.account.TioAccount;
import com.tiocloud.account.data.AccountSP;
import com.watayouxiang.androidutils.page.TioActivity;
import com.watayouxiang.db.dao.CurrUserTableCrud;
import com.watayouxiang.db.prefernces.TioDBPreferences;
import com.watayouxiang.httpclient.callback.TioCallback;
import com.watayouxiang.httpclient.callback.TioCallbackImpl;
import com.watayouxiang.httpclient.model.request.OnekeyRegisterReq;
import com.watayouxiang.httpclient.model.request.UserCurrReq;
import com.watayouxiang.httpclient.model.response.OnkeyLoginResp;
import com.watayouxiang.httpclient.model.response.UserCurrResp;

public class InputNickActivity extends TioActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inputnick);
        OnkeyLoginResp onkeyLoginResp = (OnkeyLoginResp) getIntent().getSerializableExtra("onkey");

        inputNick(onkeyLoginResp);
    }

    //未注册用户，输入昵称
    private void inputNick(OnkeyLoginResp onkeyLoginResp) {
        new InputNickDialog(new InputNickDialog.OnBtnListener() {
            @Override
            public void onClickPositive(View view, String submitTxt, InputNickDialog dialog) {
                if (TextUtils.isEmpty(submitTxt.trim())){
                    ToastUtils.showShort("昵称不能为空");
                    return;
                }
                dialog.dismiss();
                showLoadingDialog("");
                toRegiest(submitTxt.trim(), onkeyLoginResp);
//                postCreateGroup(submitTxt, uidArray, dialog);
            }

            @Override
            public void onClickNegative(View view, InputNickDialog dialog) {
                super.onClickNegative(view, dialog);
                ActivityUtils.finishAllActivities();
            }
        }).show_unCancel(this);
    }

    private void toRegiest(String submitTxt, OnkeyLoginResp onkeyLoginResp) {
        OnekeyRegisterReq onekeyRegisterReq =
                new OnekeyRegisterReq(onkeyLoginResp.getTempId(), onkeyLoginResp.getToken(), submitTxt);
        onekeyRegisterReq.setCancelTag(this);
        onekeyRegisterReq.post(new TioCallback<String>() {
            @Override
            public void onTioSuccess(String s) {
                loginStep2();
            }

            @Override
            public void onTioError(String msg) {
                ToastUtils.showShort(msg);
            }
        });
    }

    private void loginStep2() {
        // 获取用户信息
        new UserCurrReq().setCancelTag(this).get(new TioCallbackImpl<UserCurrResp>() {
            @Override
            public void onTioSuccess(UserCurrResp userCurrResp) {
//                proxy.onSuccess(userCurrResp);
//                proxy.onFinish();
                login(userCurrResp, userCurrResp.loginname);
            }

            @Override
            public void onTioError(String msg) {
                ToastUtils.showShort(msg);
//                proxy.onFailure(msg);
//                proxy.onFinish();
            }
        });
    }

    public static void login(UserCurrResp currInfo, String account) {
        // 存储登录名
        AccountSP.putLoginName(account);
        // 存储当前uid
        TioDBPreferences.saveCurrUid(currInfo.id);
        // 存储用户信息
        CurrUserTableCrud.insert(currInfo);
        // 打开主页
        TioAccount.getBridge().startMainActivity(Utils.getApp());
        // 关闭其他页面
        ActivityUtils.finishAllActivities();
    }
    private ProgressDialog mProgressDialog;
    public void showLoadingDialog(String hint) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        }
        mProgressDialog.setMessage(hint);
        mProgressDialog.setCancelable(true);
        mProgressDialog.show();
    }

    public void hideLoadingDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }
}
